$(function(){
	getAllAct();
});

function getAllAct(){
		$('#activityTable').bootstrapTable({
			method : 'get',
			url : "/showActivity",
			striped : true, 
			pageNumber : 1, 
			pagination : true,
			sidePagination : 'client',
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			showRefresh : true,
			smartDisplay:true,
			detailView:true,
			columns : [ 
	        {
				title : '商品id',
				field : 'id',
				align : 'center', 
				width:"20px",
				visible:false,  
			},{
				title : '活动id',
				field : 'marketBo.id',
				align : 'center', 
				width:"20px",
				visible:false,
			},
				{
				title : '商品名',
				field : 'title',
				align : 'center',
				width:"20px",
			}, {
				title : '活动名',
				field : 'marketBo.marketName',
				align : 'center',
				width:"20px",
			},{
				title : '商品图片',
				field : 'imgUrl',
				align : 'center',
				formatter: function (value, row, index) {
	                var value='<img style="width:80px;height:auto" src="' + row.imgUrl + '" />';
					return value;
	            },
	            width:"20px",
			}, {
				title : '商品描述',
				field : 'description',
				align : 'center',
				width:"20px",
			}, {
				title : '商品价格',
				field : 'price',
				align : 'center',
				width:"20px",
			}, {
				title : '活动价格',
				field : 'marketBo.marketItemPrice',
				align : 'center',
				width:"20px",
			}, {
				title : '活动开始时间',
				field : 'marketBo.startDate',
				align : 'center',
				width:"20px",
				formatter : function(value, row, index) {
					var time=new Date(value);
					var month=1+time.getMonth();
					var uptime=time.getFullYear();
					if(month<=9){
						uptime=uptime+"-0"+ month;
					}else{
						uptime=uptime+"-"+ month;
					}
					if(time.getDate()<=9){
						uptime=uptime+"-0"+time.getDate();
					}else{
						uptime=uptime+"-"+time.getDate();
					}
					if(time.getHours()<=9){
						uptime=uptime+"  0"+time.getHours()+":";
					}else{
						uptime=uptime+" "+time.getHours()+":";
					}
					if(time.getMinutes()<=9){
						uptime=uptime+"0"+time.getMinutes();
					}else{
						uptime=uptime+time.getMinutes();
					}
					if(time.getSeconds()<=9){
						uptime=uptime+":0"+time.getSeconds();
					}else{
						uptime=uptime+":"+time.getSeconds();
					}
					return uptime;
				},
			} , {
				title : '活动结束时间',
				field : 'marketBo.endDate',
				align : 'center',
				width:"20px",
				formatter : function(value, row, index) {
					var time=new Date(value);
					var month=1+time.getMonth();
					var uptime=time.getFullYear();
					if(month<=9){
						uptime=uptime+"-0"+ month;
					}else{
						uptime=uptime+"-"+ month;
					}
					if(time.getDate()<=9){
						uptime=uptime+"-0"+time.getDate();
					}else{
						uptime=uptime+"-"+time.getDate();
					}
					if(time.getHours()<=9){
						uptime=uptime+" 0"+time.getHours()+":";
					}else{
						uptime=uptime+" "+time.getHours()+":";
					}
					if(time.getMinutes()<=9){
						uptime=uptime+"0"+time.getMinutes();
					}else{
						uptime=uptime+time.getMinutes();
					}
					if(time.getSeconds()<=9){
						uptime=uptime+":0"+time.getSeconds();
					}else{
						uptime=uptime+":"+time.getSeconds();
					}
					return uptime;
				},
			}, {
				title : '操作',
				width : 123,
				field : 'operate',
				align : 'center',
				width:"20px",
				formatter : function(value, row, index) {
	                var value =
	                "<a href='javascript:;' onclick='updateActivity("+row.id+")'>修改</a>"+"   "+
	                "<a href='javascript:;' onclick='deleteActivity("+row.marketBo.id+")'>删除</a>";
					return value;
				}
			} ],
			onExpandRow: function (index, row, $detail) {
	            oInit.InitSubTable(index, row, $detail);
	        }
		})
}

function deleteActivity(activityId) {
	if (confirm('是否删除？')) {
        $.ajax({
            url: '/delActivity',
            type: "POST",
            data: {
                activityId:activityId
            },
            success: function (data) {
            	 if(data.status=="fail"){
            		 alert(data.data);
            		 location.reload(false);   
            	 }else{
            		 alert(data.data);
            		 location.reload(false);
            	 }
            }
        });
    }
}


function updateActivity(itemId){
	 layer.open({
			type : 2,
			title : '活动修改',
			maxmin : true,
			shadeClose : false, // 点击遮罩关闭层
			/*area : [ '850px', '550px' ],*/
           area: [ '50%', '95%' ],
			content :"/updateActivity?itemId="+itemId+"&flag=2"
		});
}